//
//  MainViewController.m
//  ICrEAM
//
//  Created by mac on 16/10/14.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "MainViewController.h"

#import "AppointmentViewController.h"
#import "GetCarViewController.h"
#import "ProxyViewController.h"
#import "ICMBalanceResearchViewController.h" //结算
#import "PushInfoTableViewCell.h"
#import "InfoTableViewCell.h"
#import "IWIconButton.h"

#import "LicencePrefixAction.h"
#import "LicencePrefixRespose.h"
#import "ICreamCache.h"
#import "ICMGetCarSearchViewController.h"

#import "MonitorViewController.h"

#import "ICFileViewController.h"

#import "ProvinceCityAction.h"
#import "ProvinceCityRespose.h"
#import "ICrEAMUser.h"
#import "CofigViewController.h"
#import "AppDelegate.h"
#import "ICrEAMTool.h"

#import "FinishCheckedViewController.h"
#import "MororcadeViewController.h"//车间进度
#import "RepaircadeViewController.h"

#import "SuspendView.h"
#import "ParameterAction.h"
#import "ParameterRespose.h"
#import "ICMAppointmentResearchViewController.h"
#import "ICMTomorrowPrepareViewController.h"
#import "RepairTypeAction.h"
#import "RepairTypeRespose.h"
#import "MainPointAction.h"
#import "MainPointRespose.h"
#import "NSObject+Propertys.h"

#import "OwnerCarListViewController.h"//车主车辆信息list
#import "RepairHistoryViewController.h"
#import "BrandSeriesResponse.h"

#import "ICMMaintenanceProjectListViewController.h"
#import "SetupViewController.h"

#import "ICMRepairPartsListViewController.h"
#import "RepairOrderTypeRespose.h"

#import "IndustryAction.h"
#import "IndustryResponse.h"
#import "qcViewController.h"
@interface MainViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIImageView *mainImageView;

@property(nonatomic,strong)UIView *menuView;
@property (weak, nonatomic) IBOutlet UITableView *InfoTableView;
@property (weak, nonatomic) IBOutlet UITableView *pushInfoTableView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;//用户名
@property(nonatomic,strong)UIButton *menuBtn;
@property(nonatomic,strong)NSArray *pushInfoTitleArr;
@property(nonatomic,strong)NSArray *infoTitleArr;
@property(nonatomic,strong)NSArray *pushInfodetailTitleArr; //关键节点信息数组
@property(nonatomic,strong)NSArray *infoTitldetaileArr;

@property (weak, nonatomic) IBOutlet UIButton *setUpBtn;

//大众和斯柯达的区分menu
@property (weak, nonatomic) IBOutlet UIImageView *titleImg;
@property (weak, nonatomic) IBOutlet UIView *InfoTitleView;
@property (weak, nonatomic) IBOutlet UIButton *firstBtn;
@property (weak, nonatomic) IBOutlet UIButton *secondBtn;
@property (weak, nonatomic) IBOutlet UIButton *thirdBtn;
@property (weak, nonatomic) IBOutlet UIButton *fourBtn;
@property (weak, nonatomic) IBOutlet UIButton *fiveBtn;
@end

//1. 服务顾问 车间进度看板没有
//2.车间主管  只有车间进度看板

@implementation MainViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.hideSuspendView=NO;
    if (![ICrEAMTool isVW]) {
        self.InfoTitleView.backgroundColor=KSKBgColor;
        self.titleImg.image=[UIImage imageNamed:@"sydlu_skt"];
        [self.setUpBtn setImage:[UIImage imageNamed:@"setUp_sk"] forState:UIControlStateNormal];
        [self.firstBtn setImage:[UIImage imageNamed:@"syico_skt"] forState:UIControlStateNormal];
        [self.secondBtn setImage:[UIImage imageNamed:@"syico02_skt"] forState:UIControlStateNormal];
        [self.thirdBtn setImage:[UIImage imageNamed:@"syico04_skt"] forState:UIControlStateNormal];
        [self.fourBtn setImage:[UIImage imageNamed:@"syico03_skt"] forState:UIControlStateNormal];
        [self.fiveBtn setImage:[UIImage imageNamed:@"syico05_skt"] forState:UIControlStateNormal];
        self.mainImageView.image=[UIImage imageNamed:@"sytp_sk"];
    }
//    [self.mainImageView sd_setImageWithURL:[NSURL URLWithString:@"http://10.180.6.208:8080/icream/20161205/VW311002/ddeb272e-458d-40fb-a5c5-656f62bcbbde.jpeg"]];
    self.edgesForExtendedLayout=UIRectEdgeNone;
    self.infoTitleArr=@[@"车  辆：",@"状  态：",@"预计交付：",@"距离交车："];
    if ([ICrEAMTool isVW]) {
           self.pushInfoTitleArr=@[@"1小时内待进站",@"15分钟内应交车",@"待提交接车单",@"待提交委托书",@"待派工",@"待终检",@"待结算"];
    }else{
           self.pushInfoTitleArr=@[@"预约未进站",@"即将交车",@"待提交接车单",@"待提交委托书",@"待派工",@"待终检",@"待结算"];
    }

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(homepageDidChange:) name:HomePageNotification object:nil];
    [self.InfoTableView reloadData];
    [self.pushInfoTableView reloadData];
    [self setupNav];
    [self requstLicencePrefixdata];
    [self requstProvinceCitydata];
    [self requstParameterdata];
    [self requstRepairTypedata];
    [self requstBrandSeriesdata];
    [self reqeustRepairOrderTypes];
    //获取大分类小分类 信息
    [self requestIndustry];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(globalTapFunctions:) name:SuspendViewClickNotification object:nil];
    
    
    //    [ICrEAMTool ipadPrintWithPrintType:GetCarPrintType printId:@"RV1612010029" Controller:self];
    //    [ICrEAMTool ipadPrintWithPrintType:ProxyPrintType printId:@"201612-0014" Controller:self];
    //    [ICrEAMTool ipadPrintWithPrintType:BalancePrintType printId:@"BO1612020016" Controller:self];
    //    return;
    
}
//获取用户最近操作的车刷新首页
-(void)homepageDidChange:(NSNotification *)notice{
    NSDictionary *dic=notice.userInfo;
    self.infoTitldetaileArr=dic[@"homepageArr"];
    self.nameLabel.text=dic[@"userName"];
    [self.InfoTableView reloadData];
 
}
#pragma mark  获取关键信息节点
-(void)requstMainPoint{
//    if ([ICrEAMUser shareICrEAMUser].userType==WorkshopType) {
//        return;
//    }
    MainPointAction *action=[[MainPointAction alloc]init];
    action.consultant=[ICrEAMUser shareICrEAMUser].employee_NO;
    [self requestDataWithAction:action haveHud:YES showError:YES  success:^(__kindof YTKBaseRequest *request) {
        MainPointRespose *mainRespose=[MainPointRespose mj_objectWithKeyValues:request.responseJSONObject];
        
        self.pushInfodetailTitleArr=[mainRespose getPropertyArray];
        [self.pushInfoTableView reloadData];
    } failure:^(__kindof YTKBaseRequest *request) {
        [self showText:@"努力加载中"];
    }];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
      [self requstMainPoint];
}
#pragma mark 获取维修类型排序
-(void)reqeustRepairOrderTypes{
    RepairTypeAction *action=[[RepairTypeAction alloc]init];
    action.isRepairOrderType=YES;
    [self requestDataWithAction:action haveHud:YES showError:NO  success:^(__kindof YTKBaseRequest *request) {
        id  keyValues = [request.responseString mj_JSONObject];
        if (![keyValues isKindOfClass:[NSDictionary class]]) {
            
            return;
        }
        RepairOrderTypeRespose *repose=[RepairOrderTypeRespose mj_objectWithKeyValues:keyValues];
        
        [[ICreamCache sharedInstance] saveArray:repose.orderTypes forKey:RepairOrderTypePath];
        
    } failure:^(__kindof YTKBaseRequest *request) {
        
    }];
}
#pragma mark 获取字典表参数
-(void)requstParameterdata{
    ParameterAction *newAction=[[ParameterAction alloc]init];
    [self requestDataWithAction:newAction haveHud:YES showError:NO  success:^(__kindof YTKBaseRequest *request) {
        
        ParameterRespose *newsRespose = [ParameterRespose mj_objectWithKeyValues:request.responseJSONObject];
        [[ICreamCache sharedInstance] saveArray:newsRespose.allParameters forKey:DictionaryPath];
        
    } failure:^(__kindof YTKBaseRequest *request) {
        
    }];
    
}
#pragma 获取维修类型
-(void)requstRepairTypedata{
    
    RepairTypeAction *newAction=[[RepairTypeAction alloc]init];
    
    [newAction startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest *request) {
        id  keyValues = [request.responseString mj_JSONObject];
        if (![keyValues isKindOfClass:[NSDictionary class]]) {
            
            return;
        }
        RepairTypeRespose *newsRespose = [RepairTypeRespose mj_objectWithKeyValues:request.responseJSONObject];
        [[ICreamCache sharedInstance] saveArray:newsRespose.JSONObjectList forKey:RepairTypePath];
            
    } failure:^(__kindof YTKBaseRequest *request) {
        
    }];
}
#pragma 获取品牌车系
-(void)requstBrandSeriesdata{
    
    LicencePrefixAction *newAction=[[LicencePrefixAction alloc]init];
    newAction.isSeries=YES;
    [newAction startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest *request) {
        id  keyValues = [request.responseString mj_JSONObject];
        if (![keyValues isKindOfClass:[NSDictionary class]]) {
            
            return;
        }
        BrandSeriesResponse *newsRespose = [BrandSeriesResponse mj_objectWithKeyValues:request.responseJSONObject];
        [[ICreamCache sharedInstance] saveArray:newsRespose.brands forKey:BrandSeriesPath];
        
        
    } failure:^(__kindof YTKBaseRequest *request) {
        
    }];
}
#pragma 获取车牌号前缀
-(void)requstLicencePrefixdata{
    
    LicencePrefixAction *newAction=[[LicencePrefixAction alloc]init];
    
    [newAction startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest *request) {
        id  keyValues = [request.responseString mj_JSONObject];
        if (![keyValues isKindOfClass:[NSDictionary class]]) {
            
            return;
        }
        LicencePrefixRespose *newsRespose = [LicencePrefixRespose mj_objectWithKeyValues:request.responseString];
        
        
        [[ICreamCache sharedInstance] saveArray:newsRespose.licencePrefixArray forKey:BrandPath];
        
        
    } failure:^(__kindof YTKBaseRequest *request) {
        
    }];
}
#pragma mark 获取省份城市
-(void)requstProvinceCitydata{
    ProvinceCityAction *newAction=[[ProvinceCityAction alloc]init];
    
    [newAction startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest *request) {
        id  keyValues = [request.responseString mj_JSONObject];
        if (![keyValues isKindOfClass:[NSDictionary class]]) {
            
            return;
        }
        ProvinceCityRespose *newsRespose = [ProvinceCityRespose mj_objectWithKeyValues:request.responseString];
        
        [[ICreamCache sharedInstance] saveArray:newsRespose.JSONArrayList forKey:ProvinceCityPath];
        
    } failure:^(__kindof YTKBaseRequest *request) {
        
    }];
}

#pragma mark 获取大分类小分类 信息
- (void)requestIndustry
{
    IndustryAction *newAction = [[IndustryAction alloc] init];
    [newAction startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest *request) {
        id  keyValues = [request.responseString mj_JSONObject];
        if (![keyValues isKindOfClass:[NSDictionary class]]) {
            
            return;
        }
        IndustryResponse *newsRespose = [IndustryResponse mj_objectWithKeyValues:request.responseJSONObject];
        [[ICreamCache sharedInstance] saveArray:newsRespose.TM_INDUSTRY forKey:IndustryPath];
        
        
    } failure:^(__kindof YTKBaseRequest *request) {
        
    }];
}


#pragma mark 设置导航栏
-(void)setupNav{
    UIView *nav=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 100)];
    //    UIView *bg=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 20)];
    //    bg.backgroundColor=[UIColor blackColor];
    //    bg.alpha=0.2;
    //    [nav addSubview:bg];
    nav.backgroundColor=[UIColor colorWithHexString:@"fcfcfc"];
    UIImageView *titleImge=[[UIImageView alloc]initWithFrame:CGRectMake(44, 0, 105, 33)];
    titleImge.centerY=nav.centerY+20/2;
    titleImge.image=[UIImage imageNamed:@"sytplogo1"];
    UIImageView *logoImge=[[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth-45-61, 0, 61, 53)];
    logoImge.centerY=nav.centerY+20/2;
    if ([ICrEAMTool isVW]) {
        logoImge.image=[UIImage imageNamed:@"sytplogo2"];
    }else{
        logoImge.image=[UIImage imageNamed:@"skdplogo2"];
    }
    
    [nav addSubview:titleImge];
    [nav addSubview:logoImge];
    [self.view addSubview:nav];
    
    
}
#pragma mark  点击主页面的menu菜单
- (IBAction)homepageMenuClick:(IWIconButton *)sender {
    if ([ICrEAMUser shareICrEAMUser].userType==WorkshopType) {
        return;
    }
    switch (sender.tag) {
        case CustomSearchMenuType:
        {
#pragma mark 进入车主车辆信息列表界面
            OwnerCarListViewController *vc = [[OwnerCarListViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
            break;
        }
        case HistorySearchMenuType:
        {
#pragma mark 维修历史查询
            RepairHistoryViewController *historyViewController = [[RepairHistoryViewController alloc]init];
            [self.navigationController pushViewController:historyViewController animated:YES];
            break;
        }
        case ProjectSearchMenuType:
        {
#pragma mark 维修项目列表界面
            
            ICMMaintenanceProjectListViewController *vc = [[ICMMaintenanceProjectListViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
            break;
        }
        case ComponetSearchMenuType:
        {
 #pragma mark 维修零件查询
            ICMRepairPartsListViewController *partsViewController = [[ICMRepairPartsListViewController alloc]init];
            [self.navigationController pushViewController:partsViewController animated:YES];
            break;
        }
        case DocumentSearchMenuType:{
#pragma mark 文件查询
            ICFileViewController *file=[[ICFileViewController alloc]init];
            
            [self.navigationController pushViewController:file animated:YES];
        }break;
            
        default:
            break;
    }
}

#pragma mark 点击悬浮按钮的menu
- (void)globalTapFunctions:(NSNotification *)noti
{
    NSDictionary *dic=noti.userInfo;
    NSInteger tag=[dic[@"ctrType"] integerValue];
    
    //    [self CheckIsEdit:[ICrEAMUser shareICrEAMUser].checkIsEdit pushType:tag];
    
    [self CheckIsEdit:NO pushType:tag];
}
-(void)didclick{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark  UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (tableView==self.InfoTableView) {
        return self.infoTitleArr.count;
    }else{
        return self.pushInfoTitleArr.count;
    }
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView==self.InfoTableView) {
        InfoTableViewCell *cell=[InfoTableViewCell createInfoTableViewCell];
        if (indexPath.row%2) {
            cell.backgroundColor=[UIColor colorWithHexString:@"f3f5f4"];
        }else{
            cell.backgroundColor=[UIColor whiteColor];
        }
        cell.title.text=self.infoTitleArr[indexPath.row];
        NSString *str=self.infoTitldetaileArr[indexPath.row];
        if (str.length>2) {
            if([[str substringToIndex:2] isEqualToString:@"超时"]){
                cell.detailL.textColor=RGB(251, 179, 35);
            }
        }
        cell.detailL.text=str;
        return cell;
    }else{
        PushInfoTableViewCell *cell=[PushInfoTableViewCell createpushInfoTableViewCell];
        cell.sectiontitileLabel.text=self.pushInfoTitleArr[indexPath.row];
        cell.countLabel.text=self.pushInfodetailTitleArr[indexPath.row];
        if ([ICrEAMUser shareICrEAMUser].userType==WorkshopType) {
            if (indexPath.row!=4) {
                cell.countLabel.text=@"";
            }
        }
      
        
        return cell;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView==self.pushInfoTableView) {
        switch (indexPath.row) {
            case 0:{
                if ([ICrEAMUser shareICrEAMUser].userType==WorkshopType){
                    return;
                }
                //跳转预约
                ICMAppointmentResearchViewController *appointmentViewController=[ICMAppointmentResearchViewController new];
                
                [self.navigationController pushViewController:appointmentViewController animated:YES];
            } break;
                
            case 1:{
                if ([ICrEAMUser shareICrEAMUser].userType==WorkshopType){
                    return;
                }
                //跳转维修进度看板
                RepaircadeViewController *repaircadeViewController=[RepaircadeViewController new];
                [self.navigationController pushViewController:repaircadeViewController animated:YES];
                
            }break;
            case 2:
                
                if ([ICrEAMUser shareICrEAMUser].userType==WorkshopType){
                    return;
                }
                //跳转接车
                [self.navigationController pushViewController:[ICMGetCarSearchViewController new] animated:YES];
                break;
            case 3:
                if ([ICrEAMUser shareICrEAMUser].userType==WorkshopType){
                    return;
                }
                //跳转委托书
                [self.navigationController pushViewController:[ProxyViewController new] animated:YES];
                break;
            case 4:{
                if ([ICrEAMUser shareICrEAMUser].userType==ConsultantType){
                    return;
                }
                //跳转维修进度看板
                RepaircadeViewController *repaircadeViewController=[RepaircadeViewController new];
                [self.navigationController pushViewController:repaircadeViewController animated:YES];
                
            } break;
            case 5:{
                if ([ICrEAMUser shareICrEAMUser].userType==WorkshopType){
                    return;
                }
                //跳转终检
                [self.navigationController pushViewController:[FinishCheckedViewController new] animated:YES];
            }break;
            case 6:
                if ([ICrEAMUser shareICrEAMUser].userType==WorkshopType){
                    return;
                }
                //跳转结算
                [self.navigationController pushViewController:[ICMBalanceResearchViewController new] animated:YES];
                break;
            default:
                break;
        }
        
    }
    
}
#pragma mark 点击首页的设置按钮
- (IBAction)setUp:(id)sender {
    
    //    [ICrEAMTool ipadPrintWithPrintType:BalancePrintType printId:@"BO1611150001" Controller:self];
    //    return;
    SetupViewController *alterSetUpCtr=[[SetupViewController alloc]init];
    __weak typeof(alterSetUpCtr) weakInner=alterSetUpCtr;
    [alterSetUpCtr setDismissBlock:^{
        
        [weakInner removeFromParentViewController];
    }];
    [ alterSetUpCtr showOnView:[[UIApplication sharedApplication] keyWindow]];
    [self addChildViewController:alterSetUpCtr];
}

- (IBAction)qc:(UIButton *)sender {
    qcViewController *vc = [[qcViewController alloc] init];
    
    [self.navigationController pushViewController:vc animated:YES];
}

@end
